/** WebWorker */
import * as constants from '@/config/constants';
import store from './store';
import Pen from './index';
import PenSocket from './pen.socket';

class PenWorker {
  constructor() {
    self.onmessage = (e) => {
      const { command, data } = e.data;
      // console.log('worker', e.data)
      switch (command) {
        case constants.WORKER_INIT: {
          console.log('笔的WORKER初始化')
          store.pen = new Pen(data.canvas);
          store.pen.setBook(JSON.parse(data.bookMapData))
          store.socket = new PenSocket(data.ws);
          store.mac = data.mac;
          // store.timer = setInterval(() => {
          //   const nowDate = Date.now()
          //   console.log(store.mac, nowDate)
          //   for (const key in store.tempPenData) {
          //     if (nowDate - store.tempPenData[key].lastWriteTime > 3000) {
          //       console.log(store.tempPenData[key], key)
          //       store.socket.returnPenData(store.tempPenData[key])
          //     }
          //   }
          // }, 1000)
          break;
        }
        case constants.WORKER_GET_OFFLINE_SIZE_NUMBER: {
          store.socket.getOfflineDataNumber();
          break;
        }
        case constants.WORKER_START_OFFLINE_TRANSFER: {
          store.socket.startOfflineTransfer();
          break;
        }
        case constants.WORKER_PAUSE_OFFLINE_TRANSFER: {
          store.socket.pauseOfflineTransfer();
          break;
        }
        case constants.WORKER_CONTINUE_OFFLINE_TRANSFER: {
          store.socket.continueOfflineTransfer();
          break;
        }
        case constants.WORKER_STOP_OFFLINE_TRANSFER: {
          store.socket.stopOfflineTransfer();
          break;
        }
        case constants.WORKER_PEN_BATTERY: {
          store.socket.getBattery();
          break;
        }
        case constants.TEST: {
          store.socket.test(data);
          break;
        }
        case constants.WORKER_CLEAR_SCREEN: {
          store.pen.drawBackground();
          break;
        }
        case constants.WORKER_GET_AUTO_OFF: {
          store.socket.getAutoOffTime();
          break;
        }
        case constants.WORKER_SET_AUTO_OFF: {
          store.socket.setAutoOffTime(data);
          break;
        }
        case constants.GET_CODE_POINT_VALUE: {
          store.socket.getCodePointValue();
          break;
        }
        case constants.PEN_DATA: {
          postMessage(constants.PEN_DATA, data);
          store.socket.getCodePointValue();
          break;
        }
        // 更新笔里的教材信息
        case constants.UPDATE_BOOK_DATA: {
          store.pen.setBook(JSON.parse(data.bookMapData))
          break;
        }
        default:
          break;
      }
    };
    self.onerror = (e) => {
      console.error('worker-onerror', e);
    };
    self.onmessageerror = (e) => {
      console.error('worker-onmessageerror', e);
    };
  }
  // 向主线程发送数据
  postMessage(command, data) {
    self.postMessage({ command, data });
  }
}

store.worker = new PenWorker();
